//
//  SNVideoCell.m
//  DayDayHappy
//
//  Created by yutao on 14-2-21.
//  Copyright (c) 2014年 yutao. All rights reserved.
//

#import "SNVideoCell.h"
#define kCellContentHeight 200
@interface SNVideoCell()
@property (nonatomic,copy)NSString *videoUrl;
@end
@implementation SNVideoCell
@synthesize delegate;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        contentImageView.clipsToBounds = YES;
        contentImageView.contentMode = UIViewContentModeScaleAspectFill;

        playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [playBtn setFrame:CGRectMake(0, 0, 67, 67)];
        [playBtn setImage:[UIImage imageNamed:@"video_cell_videoIcon.png"] forState:UIControlStateNormal];
        playBtn.center = CGPointMake(150, 100);
        [playBtn addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
        [contentImageView addSubview:playBtn];
    }
    return self;
}

- (void)setUpImageCellWithObject:(SNVideoModel*)imageModel atIndexPath:(NSIndexPath*)indexPath
{
    [super setUpImageCellWithObject:imageModel atIndexPath:indexPath];
    self.videoUrl = imageModel.video;
    
    [contentImageView setFrame:CGRectMake(10, CGRectGetMinY(txtContent.frame) + CGRectGetHeight(txtContent.frame) +10, kCellContentWidth, kCellContentHeight)];
    //@"http://ww2.sinaimg.cn/large/7cc82f27jw1ec0qtch95uj20e609fwfe.jpg"
    [contentImageView setImageWithURL:[NSURL URLWithString:imageModel.image] placeholderImage:[UIImage imageNamed:@""] options:SDWebImageRefreshCached];
    
    [self setFrame:CGRectMake(10, 0, kCellContentWidth, CGRectGetMinY(contentImageView.frame) + CGRectGetHeight(contentImageView.frame) + 20)];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}



- (void)addPlayerObservers
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playVideoFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:playerController];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playVideoFullScreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:playerController];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playVideoExitFullScreen:) name:MPMoviePlayerDidExitFullscreenNotification object:playerController];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playVideoLoadStateChange:) name:MPMoviePlayerLoadStateDidChangeNotification object:playerController];
}

-(void)play:(UIButton *)button
{
    
    //可以播放的   ==self.videoUrl======

//http://f5.r.56.com/f5.c114.56.com/flvdownload/8/14/sc_137692238977hd.flv.mp4?v=1&t=yUGYvpDRCXHO8dAqoejaVw&r=12741&e=1394202971&tt=333&sz=17863830&vid=95481575
//http://f5.r.56.com/f5.c114.56.com/flvdownload/8/14/sc_137692238977hd.flv.mp4?v=1&t=CCu-jhfCpcUrilnY4uuYQw&r=14746&e=1394298330&tt=333&sz=17863830&vid=95481575
    
    NSLog(@"==self.videoUrl======%@",self.videoUrl);
    if ([self.delegate respondsToSelector:@selector(videoCellDidClick:)]) {
        [self.delegate videoCellDidClick:self];
    }
    if (!playerController) {
        playerController = [[SNAutorotateMoviePlayerController alloc] init];
        playerController.controlStyle = MPMovieControlStyleFullscreen;
        playerController.scalingMode = MPMovieScalingModeAspectFit;
        playerController.view.frame = CGRectMake(0,  0, 300, 200);
//        [playerController setContentURL:[NSURL URLWithString:@"http://f5.r.56.com/f5.c114.56.com/flvdownload/8/14/sc_137692238977hd_qqvga.mp4?v=1&t=JENlqdm2R2w46kispgfOQQ&r=18726&e=1394445259&tt=333&sz=3854769&vid=95481575"]];
//        [playerController setContentURL:[NSURL URLWithString:@"http://f5.r.56.com/f5.c114.56.com/flvdownload/8/14/sc_137692238977hd.flv.mp4?v=1&t=cej5nxUUlTrMN0GHFoKN8g&r=18726&e=1394445259&tt=333&sz=17863830&vid=95481575"]];
        
//        [playerController setContentURL:[NSURL URLWithString:@"http://f5.r.56.com/f5.c114.56.com/flvdownload/8/14/sc_137692238977hd.flv.m3u8?v=1&t=uXPyIHpJ4lbD101uDQHjbg&r=18726&e=1394445259&tt=0&sz=0&vid=95481575"]];
        
        [playerController setContentURL:[NSURL URLWithString:@"http://f5.r.56.com/f5.c114.56.com/flvdownload/8/14/sc_137692238977hd.flv.m3u8?v=1&t=MW7inuPmrP0TLL7qbKUxig&r=85278&e=1394545201&tt=0&sz=0&vid=95481575"]];
        [contentImageView addSubview:playerController.view];
        playerController.view.hidden = YES;
        playBtn.hidden = YES;
        [self addPlayerObservers];
        [self addPlayerLoadingView];
        [playerController prepareToPlay];
        [playerController play];
    }
}
#define kTagArticlePlayerLoading 199
- (UIActivityIndicatorView *)playerLoadingView {
    UIActivityIndicatorView *playerLoadingView = (UIActivityIndicatorView *)[contentImageView viewWithTag:kTagArticlePlayerLoading];
    if (playerLoadingView == nil) {
        playerLoadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        playerLoadingView.center = playBtn.center;
        playerLoadingView.tag = kTagArticlePlayerLoading;
        [contentImageView addSubview:playerLoadingView];
    }
    return playerLoadingView;
}
- (void)addPlayerLoadingView {
    [[self playerLoadingView] startAnimating];
}

- (void)removePlayerLoadingView {
    [[self playerLoadingView] stopAnimating];
}

- (void) playVideoFinished:(NSNotification *)theNotification//当播放完毕时调用此函数
{
    [self removePlayer];
    playBtn.hidden = NO;
    [self performSelector:@selector(resetStatusBar) withObject:nil afterDelay:0.2];
    
}
-(BOOL)isiOS7{
    return ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7);
}
- (void)resetStatusBar
{
    if ([self isiOS7]) {
        [UIApplication sharedApplication].statusBarHidden = NO;
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    }else{
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
    }
}

- (void) playVideoFullScreen:(NSNotification *)theNotification//当播放完毕时调用此函数
{
    
}

- (void) playVideoExitFullScreen:(NSNotification *)theNotification
{
    
    // 全屏点完成，停止播放
    if (playerController.view.superview
        && playerController.playbackState == MPMoviePlaybackStatePaused) {
        [self removePlayer];
        playBtn.hidden = NO;
        [self performSelector:@selector(resetStatusBar) withObject:nil afterDelay:0.0];
        
    }
}

- (void) playVideoLoadStateChange:(NSNotification *)theNotification
{
    [self removePlayerLoadingView];
    [playerController view].hidden = NO;
    if ([self isiOS7]) {
        [UIApplication sharedApplication].statusBarHidden = YES;
    }
    [playerController setFullscreen:YES animated:YES];
}

- (void)removePlayerObservers
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:playerController];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerWillEnterFullscreenNotification object:playerController];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:playerController];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:playerController];
}

- (void)removePlayer
{
    [playerController setFullscreen:NO];
    [[playerController view] removeFromSuperview];
    playerController = nil;
    [self removePlayerObservers];
    [self removePlayerLoadingView];
}

- (void)stopPlayerController
{
    [playerController stop];
    playBtn.hidden = NO;
    [playerController setContentURL:nil];
    [self removePlayer];
}

- (void)didMoveToWindow {
    if (SYSTEM_VERSION_LESS_THAN(@"6.0")) {
        if (self.window == nil) {
            // I have been removed from the table view.
            NSLog(@"I have been removed from the table view.");
            [self stopPlayerController];
        }
    }
    
}

@end
